﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebBanThuoc.Function;
using WebBanThuoc.Server;
using WebBanThuoc.DAO;
using WebBanThuoc.DTO;

namespace WebBanThuoc.admin
{
    public partial class QLNoiDungHienThi : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!FuncAdmin.IsLogin(Session))
            {
                Session.Add("URLCurrentActive_SendTo_Login", Request.RawUrl);
                Response.Redirect("/admin/DangNhap.aspx");
            }



            if (!IsPostBack)
            {
                getData(true);


            }
            else
            {
                if (checkDefaultSubmit())
                {
                    getData(false);
                }
                else
                {
                    //if (Request.Params["btn_Them"] != null)
                    //{
                    //    addProduct();
                    //}
                    if (Request.Params["btn_Sua"] != null)
                    {
                        updateProduct();
                    }
                }
            }

            this.LoadComplete += new EventHandler(QLNoiDungHienThi_LoadComplete);
        }

        void QLNoiDungHienThi_LoadComplete(object sender, EventArgs e)
        {
            IServerWeb sw = new ServerWeb();
            NoiDungHienThi_DAO dao = new NoiDungHienThi_DAO(sw.Connection);
            List<NoiDungHienThi> data = dao.getListByKey();
            Session.Add("page_NoiDungHienThi_list", data);
        }

        private bool checkDefaultSubmit()
        {
            if ( Request.Params["btn_Sua"] != null)
            {
                return false;
            }
            return true;
        }

        private void getData(bool isBegin)
        {
            string MaNoiDung = Request.QueryString["MaNoiDung"];
            if (MaNoiDung == null)
            {
                MaNoiDung = Request.Params["MaNoiDung"];
            }
            IServerWeb sw = new ServerWeb();
            NoiDungHienThi_DAO dao = new NoiDungHienThi_DAO(sw.Connection);
            NoiDungHienThi data = null;
            if (isBegin)
            {
                data = dao.getListByKey()[0];
            }
            else
            {
                if (MaNoiDung != null && MaNoiDung.Trim() != "" && !isBegin)
                {
                    data = dao.getByKey(MaNoiDung);                    
                }
            }
            if (data != null)
            {
                Session.Add("page_NoiDungHienThi_dataND", data);

                NoiDung.Text = data.NoiDung;
            }
            else
            {
                NoiDung.Text = "";
            }
            sw.closeConnection();
        }

        private void updateProduct()
        {
            string MaNoiDung = Request.Params["MaNoiDung"];
            if (MaNoiDung != null || MaNoiDung.ToString().Trim() != "")
            {

                IServerWeb sw = new ServerWeb();
                NoiDungHienThi_DAO dao = new NoiDungHienThi_DAO(sw.Connection);
                NoiDungHienThi data = dao.getByKey(MaNoiDung);
                
                data.NoiDung = NoiDung.Text;

                
                
                int tempRS = dao.updateByKey(data, data.MaNoiDung);
                //tempRS = 1;
                if (tempRS > 0)
                {
                    NoiDungHienThi spNew = dao.getByKey(data.MaNoiDung);
                    Session.Add("page_NoiDungHienThi_msgSucessFunc", "Đã Cập nhật thành công " + spNew.TenNoiDung);
                    Session.Add("page_NoiDungHienThi_dataND", spNew);
                    NoiDung.Text = spNew.NoiDung;                    
                }
                sw.closeConnection();
            }
        }

    }
}